using System;
using System.Collections.Generic;
using System.Linq;
using TickSharp.Core.Enum;
using TickSharp.Core.Exceptions;
using TickSharp.Core.Infrastructure;
using TickSharp.Core.Infrastructure.Repositories;
using TickSharp.Core.Model;
using Xunit;

namespace TickSharp.Tests.IntegrationTests.Infrastructure
{
    public class RepositoryTester : RepositoryTesterBase
    {
        private readonly EntryRepository _entryRepository;

        public RepositoryTester()
        {
            _entryRepository = new EntryRepository(GetApiConnectionInfo());
        }
        
        [Fact]
        public void ListReturnsOnlyEntriesWithGivenUserIdWhenFilterPassed()
        {
            var filterByUser = new[] {EntryFilter.Create(EntryFilterType.UserId, CharlesBarkley)};

            IEnumerable<Entry> entries = _entryRepository.List(new DateTime(2000, 1, 1), DateTime.Now, filterByUser);            

            Assert.True(entries.All(e => e.UserId == CharlesBarkley));
        }

        [Fact]
        public void ListThrowsTickspotAuthenticationExceptionWhenInvalidLoginInformationGiven()
        {
            var repository = new EntryRepository(new ApiConnectionInfo("wrong.tickspot.com", "wrong", "wrong"));
            Assert.Throws(typeof(TickspotAuthorizationException), () => repository.List(new DateTime(), new DateTime()));
        }
    }
}